package com.example.questionnaire.service.impl;

import com.example.questionnaire.dao.AnswererDao;
import com.example.questionnaire.dto.AnswererAddDto;
import com.example.questionnaire.entity.AnswererEntity;
import com.example.questionnaire.entity.UserEntity;
import com.example.questionnaire.entity.UserTeamEntity;
import com.example.questionnaire.service.AnswererService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.questionnaire.dao.UserAnswererDao;
import com.example.questionnaire.entity.UserAnswererEntity;
import com.example.questionnaire.service.UserAnswererService;


@Service("userAnswererService")
public class UserAnswererServiceImpl extends ServiceImpl<UserAnswererDao, UserAnswererEntity> implements UserAnswererService {
    @Autowired
    private UserAnswererDao userAnswererDao;
    @Autowired
    private AnswererService answererService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserAnswererEntity> page = this.page(
                new Query<UserAnswererEntity>().getPage(params),
                new QueryWrapper<UserAnswererEntity>()
        );
        page.setTotal(page.getRecords().size());

        return new PageUtils(page);
    }
    @Override
    public PageUtils queryUserAnswerer(Map<String, Object> params) {
        QueryWrapper<UserAnswererEntity>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",params.get("id"));
        List<UserAnswererEntity> list = this.list(queryWrapper);
        System.out.println("查到的结果："+list);
        QueryWrapper<AnswererEntity> wrapper = new QueryWrapper<AnswererEntity>();
        wrapper.eq("is_deleted", 0);
        List<Integer> collect = list.stream().map(item -> {
            Integer answererId = item.getAnswererId();
            wrapper.or(item1->{
                item1.eq("id",answererId);
            });
            return answererId;
        }).collect(Collectors.toList());
        wrapper.eq("is_deleted", 0);
        wrapper.eq("id",collect);
//        if(params.get("page") != null && params.get("limit") != null) {
//            int pageNum = Integer.valueOf(params.get("page").toString());
//            int limit = Integer.valueOf(params.get("limit").toString());
//            int current = (pageNum - 1) * limit;
//            String sql = "limit" + " " + current + "," + limit;
//            wrapper.last(sql);
//        }
        IPage<AnswererEntity> page = answererService.page(
                new Query<AnswererEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void addUserAnswererInfo(AnswererAddDto answererAddDto) {
        userAnswererDao.addUserAnswererInfo(answererAddDto);
    }
}